#!/usr/bin/env python

# --------------------------------------
# projects/python/primes/TestPrimes.py
# Copyright (C) 2009
# Glenn P. Downing
# --------------------------------------

# To run the tests
#     TestCollatz.py

# To document the tests
#     pydoc -w TestCollatz

import main
import unittest

# ----------
# TestReader
# ----------

class TestReader (object) :
    def __init__ (self, s) :
        self.s = s

    def read (self) :
        return self.s

# ----------
# TestWriter
# ----------

class TestWriter (object) :
    def str (self) :
        return self.s

    def write (self, *a) :
        self.s = str(a[0])
        for w in a[1:] :
            self.s += ' '
            self.s += str(w)
        self.s += '\n'

# -----------
# TestPrimes
# -----------
class TestVoting (unittest.TestCase) :
    """
    Tests the read, write, isPrime, and Eval methods of the main.py program.
    """

    # -------
    # Classes
    # -------

    def test_Ballots_initiate (self) :
        test_ballot = main.Ballot()
        test_ballot.initiate("1 2 3 4 5")
        self.assert_(test_ballot.get_Winner() == "1")

    def test_Ballots_next_winner (self) :
        test_ballot = main.Ballot()
        test_ballot.initiate("1 2 3 4 5")
        test_ballot.next_Winner()
        self.assert_(test_ballot.get_Winner() == "2")

    def test_Candidates_initiate (self) :
        test_Candidate = main.Candidate()
        test_Candidate.initiate("Jacob Roseland", 1)
        self.assert_(test_Candidate.get_Name() == "Jacob Roseland")
        self.assert_(test_Candidate.get_Number() == 1)

    def test_Candidates_not_lost (self) :
        test_Candidate = main.Candidate()
        test_Candidate.initiate("Jacob Roseland", 1)
        self.assert_(test_Candidate.not_Lost() == True)

    def test_Candidates_lost (self) :
        test_Candidate = main.Candidate()
        test_Candidate.initiate("Jacob Roseland", 1)
        test_Candidate.lost()
        self.assert_(test_Candidate.not_Lost() == False)

    def test_Candidates_add_ballot (self) :
        test_Candidate = main.Candidate()
        test_Candidate.initiate("Jacob Roseland", 1)
        test_ballot = main.Ballot()
        test_ballot.initiate("1 2 3 4 5")
        test_Candidate.add_Ballot(test_ballot)
        self.assert_(test_Candidate.get_Votes() == 1)

    # ----
    # eval
    # ----

    def test_eval1 (self) :
        can1 = main.Candidate()
        can1.initiate("John Doe", 0)
        can2 = main.Candidate()
        can2.initiate("Jane Smith", 1)
        can3 = main.Candidate()
        can3.initiate("Sirhan Sirhan", 2)

        b1 = main.Ballot()
        b1.initiate("1 2 3")
        b2 = main.Ballot()
        b2.initiate("2 1 3")
        b3 = main.Ballot()
        b3.initiate("2 3 1")
        b4 = main.Ballot()
        b4.initiate("1 2 3")
        b5 = main.Ballot()
        b5.initiate("3 1 2")

        can1.add_Ballot(b1)
        can2.add_Ballot(b2)
        can2.add_Ballot(b3)
        can1.add_Ballot(b4)
        can3.add_Ballot(b5)

        main.candidates = []
        main.candidates.append(can1)
        main.candidates.append(can2)
        main.candidates.append(can3)

        main.my_eval()
        self.assert_(main.winner == "John Doe")

    def test_eval2 (self) :
        can1 = main.Candidate()
        can1.initiate("John Doe", 0)
        can2 = main.Candidate()
        can2.initiate("Jane Smith", 1)
        can3 = main.Candidate()
        can3.initiate("Sirhan Sirhan", 2)

        b1 = main.Ballot()
        b1.initiate("1 2 3")
        b2 = main.Ballot()
        b2.initiate("2 1 3")
        b3 = main.Ballot()
        b3.initiate("2 3 1")
        b4 = main.Ballot()
        b4.initiate("1 2 3")
        b5 = main.Ballot()
        b5.initiate("2 1 3")

        can1.add_Ballot(b1)
        can2.add_Ballot(b2)
        can2.add_Ballot(b3)
        can1.add_Ballot(b4)
        can2.add_Ballot(b5)

        main.candidates = []
        main.candidates.append(can1)
        main.candidates.append(can2)
        main.candidates.append(can3)

        main.my_eval()
        self.assert_(main.winner == "Jane Smith")

    def test_eval3 (self) :
        can1 = main.Candidate()
        can1.initiate("John Doe", 0)
        can2 = main.Candidate()
        can2.initiate("Jane Smith", 1)
        can3 = main.Candidate()
        can3.initiate("Sirhan Sirhan", 2)

        b1 = main.Ballot()
        b1.initiate("3 2 1")
        b2 = main.Ballot()
        b2.initiate("2 3 1")
        b3 = main.Ballot()
        b3.initiate("3 2 1")
        b4 = main.Ballot()
        b4.initiate("1 2 3")
        b5 = main.Ballot()
        b5.initiate("2 3 1")
        b6 = main.Ballot()
        b6.initiate("3 1 2")

        can3.add_Ballot(b1)
        can2.add_Ballot(b2)
        can3.add_Ballot(b3)
        can1.add_Ballot(b4)
        can2.add_Ballot(b5)
        can3.add_Ballot(b6)

        main.candidates = []
        main.candidates.append(can1)
        main.candidates.append(can2)
        main.candidates.append(can3)

        main.my_eval()
        self.assert_(main.winner == "Sirhan Sirhan")
    

    # -----
    # print
    # -----

    def test_print (self) :
        writer = TestWriter()
        main.cases = 1
        main.winner = "Jacob Roseland"
        main.my_print(writer)
        self.assert_(writer.str() == 'Jacob Roseland\n')


if __name__ == "__main__" :
    unittest.main()